package server.content.abilities;

import java.util.LinkedList;
import java.util.List;

import server.game.players.Client;

public class BananaTreeHandler {

	public static final List<BananaTree> trees = new LinkedList<BananaTree>();

	public static void addTree(final Client c, final int objectId,
			final int[] coords) {
		if (!treeExists(coords[0], coords[1])) {
			trees.add(new BananaTree(c, objectId, coords[0], coords[1]));
		}
	}

	public void process() {
		for (final BananaTree tree : trees) {
			if (tree == null)
				continue;
			if (tree.getStage() == -1 && !tree.hasBananas()) {
				if (tree.getRespawn() > 0) {
					tree.setRespawn(tree.getRespawn() - 1);
				} else if (tree.getRespawn() == 0) {
					tree.setStage(5);
					tree.setRespawn(-1);
					tree.setHasBananas(true);
					tree.updateTree();
				}
			}
		}
	}

	public static BananaTree getTree(final int x, final int y) {
		for (final BananaTree tree : trees) {
			if (tree.getX() == x && tree.getY() == y) {
				return tree;
			}
		}
		return null;
	}

	public static boolean treeExists(final int x, final int y) {
		for (final BananaTree tree : trees) {
			if (tree.getX() == x && tree.getY() == y) {
				return true;
			}
		}
		return false;
	}

}
